#ifndef TOOLS
#define TOOLS

#include<string>
#include<strings.h>
#include<map>
#include"Defines.cpp"
#include"ClientSocketHandler.cpp"
#include"FileHandler.cpp"
 class Tools
 {
    private:
    public:
    static std::map<std::string, std::string> ContentTypeMap;
    static bool SendFile(std::string path, ClientSocketHandler *clientSocketHandler)
    {
        //打开文件
        FileHandler fileHandler(path);
        char buff[4096];
        int buffLength = 0;
        while((buffLength = fileHandler.Read(buff, sizeof(buff))) > 0)
        {
            if(clientSocketHandler->WriteSocket(buff, buffLength) == false)
            {
                PrintLine("文件发送失败");
                return false;
            }
        }
        return true;
    }

    static std::string GetExtendName(std::string str)
    {
        // test.jpg
        int index = str.length() - 1;
        while (str[index] != '.' && index >= 0)
        {
            index--;
        }
        if(index == -1)
        {
            PrintLine("no extend name");
            return "";
        }
        return str.substr(index + 1, str.size() - index - 1);
    }


    static std::string GetContentTypeByExtendName(std::string extendName)
    {
        InitContentTypeMap();
        if(ContentTypeMap.find(extendName) == ContentTypeMap.end())
        {
            return "text/html";
        }
        return ContentTypeMap[extendName];
    }

    static void InitContentTypeMap()
    {
        if(ContentTypeMap.size() != 0)
        {
            return;
        }
        ContentTypeMap.insert(std::pair<std::string, std::string>("jpg", "image/jpeg"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("jpeg", "image/jpeg"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("png", "image/png"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("gif", "image/gif"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("json", "application/json"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("mp3", "audio/mp3"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("mp4", "video/mpeg4"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("html", "text/html"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("xml", "application/xml"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("css", "text/css"));
        ContentTypeMap.insert(std::pair<std::string, std::string>("js", "application/x-javascript"));
    }

 };
 std::map<std::string, std::string> Tools::ContentTypeMap;

#endif